import 'package:flutter/material.dart';

class MyApp4 extends StatefulWidget {
  String parentName;
  MyApp4({super.key, required this.parentName});

  //1.创建组件的时候调用
  @override
  State<MyApp4> createState() {
    print('第一-----MyApp4.createState');
    return _MyApp4State();
  }
}

class _MyApp4State extends State<MyApp4> {
  int age = 0;
  String name = '张三';

  //2.组件初始化时调用
  //一般用于:对组件变量赋值,请求网络数据,更新状态
  @override
  void initState() {
    debugPrint('第二-----MyApp4.initState');
    super.initState();
    name = widget.parentName;
  }

  //3.状态组件的依赖项发生变化时调用
  //只会被执行1次
  @override
  void didChangeDependencies() {
    debugPrint('第三-----MyApp4.didChangeDependencies');
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    debugPrint('第四-----MyApp4.build');
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '有状态的声明周期组件',
          style: TextStyle(color: Colors.white, fontSize: 20),
        ),
        backgroundColor: Colors.pink,
        centerTitle: true,
      ),
      body: Center(
        child: Text(
          '$name组件生命周期$age',
          style: TextStyle(fontSize: 20, color: Colors.red),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            age++;
            print(age);
          });
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}
